using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CodeWork.DAL.BDHomePage;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_UserRoleEdit : SiteAdminBasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        UserRole userRole;

        if (Int32.TryParse(Request["id"], out id))
            userRole = UserRole.Load(id);
        else
            userRole = null;

        if (userRole != null)
        {
            if (!string.IsNullOrEmpty(userRole.RoleName))
				tbxRoleName.Text = userRole.RoleName;
			tbxRoleLevel.Text = userRole.RoleLevel.ToString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string roleName = Security.BindStringInput(tbxRoleName.Text, _securityLevel);
		int roleLevel = Security.BindIntegerInput(tbxRoleLevel.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (UserRole.Save(
				id, 
				roleName,
				roleLevel
                ))
                    Response.Redirect("userrolelist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            UserRole userRole = UserRole.Create(
				roleName,
				roleLevel
                );

            if (userRole != null)
                Response.Redirect("userrolelist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("userrolelist.aspx");
    }
}
